﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using Telerik.Web.UI;

namespace Web.SalesPipeline
{
    public partial class SalesPipeline : System.Web.UI.Page
    {
        static ROLE r;
        static USER user;
        static List<CONTACT_DETAIL> contactDetails;
        static long? assignedtoID = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedtoID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
                    assignedtoID = user.ID;
                    divAssignTo.Visible = false;
                }
                LoadAllUser(ddlAssignedTo);
                LoadEmailTemplate();
                contactDetails = getDatasource(assignedtoID, null, null);
                RadGrid1.DataBind();
            }
        }
        private void LoadEmailTemplate()
        {
            ddlEmailTemplate.DataSource = EmailTemplateService.GetEmailTemplateByCondition("", "", "");
            ddlEmailTemplate.DataBind();
            ddlEmailTemplate.Items.Insert(0, new RadComboBoxItem("None", "-1"));
        }
        protected void sendEmail_Click(object sender, EventArgs e)
        {
            LinkButton lbtnSendEmail = sender as LinkButton;
            EMAIL_TEMPLATE email = new EMAIL_TEMPLATE();
            email.SUBJECT = " ";
            email.MESSAGE = " ";
            EMAIL_TEMPLATE emailTemplate = EmailTemplateService.GetEmailTemplateByID(Convert.ToInt64(ddlEmailTemplate.SelectedValue.ToString()));
            if (emailTemplate != null)
            {
                email = emailTemplate;

            }
            string mailto = lbtnSendEmail.Text;
            string t = "parent.location='mailto:" + mailto + "?Subject=" + email.SUBJECT + "&Body=" + email.MESSAGE.Replace(" ", "%20").Replace("\r\n", "%0A") + "'";
            ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", t, true);
        }
        private void LoadAllUser(RadComboBox ddlAssignedTo)
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                }
                ddlAssignedTo.Items.Insert(0, new RadComboBoxItem("All", "-1"));
            }
        }
        private List<CONTACT_DETAIL> getDatasource(long? assignid, string finalStep, bool? value)
        {
            List<CONTACT_DETAIL> dt = ContactService.GetSalesPipeline(assignid, finalStep, value);
            if (dt == null)
                dt = new List<CONTACT_DETAIL>();
            return dt;
        }
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = contactDetails;
        }
        private bool? getNewBoolValue(bool value)
        {
            if (!value)
                return null;
            return value;
        }
        protected void btnSearch_ServerClick(object sender, EventArgs e)
        {
            string finalStep; bool? value = null;
            finalStep = ddlStatus.SelectedValue == "0" ? null : ddlStatus.SelectedValue;
            value = !rbtnAll.Checked ? rbtnYes.Checked : value;

            long? at = Convert.ToInt64(ddlAssignedTo.SelectedValue);
            assignedtoID = at == -1 ? null : at;
            contactDetails = getDatasource(assignedtoID, finalStep, value);
            RadGrid1.DataSource = contactDetails;
            RadGrid1.DataBind();
        }

        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
            {
                RadGrid1.MasterTableView.GetColumn("ASSIGN_TO").Visible = false;
                RadGrid1.MasterTableView.GetColumn("PRIMARY_EMAIL").ItemStyle.Width = Unit.Percentage(20);
                RadGrid1.MasterTableView.GetColumn("PRIMARY_EMAIL").HeaderStyle.Width = Unit.Percentage(20);
                RadGrid1.MasterTableView.GetColumn("STATUS_NOTE").ItemStyle.Width = Unit.Percentage(30);
                RadGrid1.MasterTableView.GetColumn("STATUS_NOTE").HeaderStyle.Width = Unit.Percentage(30);
            }
            foreach (GridGroupHeaderItem groupHeader in RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader))
            {
                GridDataItem item = groupHeader.GetChildItems()[0] as GridDataItem;
                string finalStep = item.GetDataKeyValue("STATUS_FINAL_STEP").ToString().Trim();
                string strtxt = groupHeader.DataCell.Text;
                string key = string.Empty;
                switch (finalStep)
                {
                    case "1":
                        key = "Appointment";
                        break;
                    case "2":
                        key = "Quotation Issue";
                        break;
                    case "31":
                        key = "Deposit Collected";
                        break;
                    case "32":
                        key = "Deposit Invoice Issue";
                        break;
                    case "4":
                        key = "Full Invoice Issue";
                        break;
                    case "5":
                        key = "Payment Collection";
                        break;
                    default:
                        key = "NULL";
                        break;
                }
                Label lbl = new Label();
                lbl.ID = "lblGroupHeader";
                lbl.Text = key;
                lbl.ForeColor = System.Drawing.Color.Blue;
                groupHeader.DataCell.Controls.Clear();
                groupHeader.DataCell.Controls.Add(lbl);
                groupHeader.ForeColor = System.Drawing.Color.White;
            } 
        }

        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
                RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
                oRadComboBox.Width = Unit.Pixel(50);
            }
        }
    }
}